﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Windows;
using System.Diagnostics;
using System.Windows.Media.Imaging;

namespace AppToolkit.WPF.UI.Converters
{
    /// <summary>
    /// Converts enum values to named bitmap resources.
    /// </summary>
    public class EnumToBitmapResourceConverter : IValueConverter
    {
        #region IValueConverter Members
        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                Trace.TraceWarning("Passed in value is null!");
                return DependencyProperty.UnsetValue;
            }

            Type t = value.GetType();
            if (t.IsEnum)
            {
                string name = Enum.GetName(t, value);
                var val = this.Mappings.FirstOrDefault(p => p.TypeName == name);

                if (val != null)
                    return val.ResourceUri;
                else
                    Trace.TraceWarning("No mapping found for enum value: {0}!", name);
            }
            else
                Trace.TraceWarning("Passed in object '{0}' is not of type enum!", t.Name);

            return DependencyProperty.UnsetValue;
        }

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// Specifies the list of mappings
        /// </summary>
        public ICollection<MappingElement> Mappings { get; set; }
    }

    /// <summary>
    /// Class that defines one mapping from enum value to ResourceUri.
    /// </summary>
    public class MappingElement
    {
        /// <summary>
        /// Specify the enum value that shall be mapped
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// the resource the enum value shall be mapped to.
        /// </summary>
        public Uri ResourceUri { get; set; }
    }

    /// <summary>
    /// Collection of mapping elements.
    /// </summary>
    public class MappingCollection : Collection<MappingElement>
    {
    }
}


